<?php
	$form = $_POST;
	$room = new Room();
	$room->setname($form['room_name']);
	$room->setprice($form['price']);
	$room->setcurrency($form['currency']);
	if (isset($form['promotion']))
		$room->setpromotion($form['promotion']);
	if (isset($form['description']))
		$room->setdescription($form['description']);
		
	foreach ($form as $key => $value)
		if (strpos($key, 'facility_') !== false && $value = 'on') {
			preg_match("#facility_([0-9]*)#", $key, $match);
			if (isset($match[1]))
				$selected_facilities[] = $match[1];
		}

	if ($room_id) { // it's an update action
		$room->update($room_id);
		$room->select($room_id);
		
		// remove all old hotel facilities firstly
		$facilities = RoomFacility::getAllFacilitiesForRoom($room_id);
		if (!empty($facilities))
			foreach ($facilities as $facility) {
				$room_facility =  RoomFacility::retriveByHotelIdAndFacilityId($room_id, $facility->getid());
				$room_facility->delete($room_facility->getid());
			}
	} else { // it's an create action
		$room->sethotel_id($hotel_id);
		$room->insert();
	}
	
	
	// create new hotel facility relations
	foreach ($selected_facilities as $facility_id) {
		$room_facility = new RoomFacility();
		$room_facility->setroom_id($room->getid());
		$room_facility->setfacility_id($facility_id);
		$room_facility->insert();
	}

	$msg = $room_id ? "Phòng đã được sửa thành công" : "Phòng đã được thêm thành công";
	admin_load($msg, "?act=rooms_list&hotel_id=".$hotel_id);
?>